<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/4/15
 * Time: 16:27
 * 待发货订单
 */
namespace app\admin\controller;
use think\Db;
use think\Request;

class Deliver extends Base {
    public function index(){
        if(request()->isAjax()){
            $params = input('param.');
            $where['order_status'] = 2;
            $where['order_after'] = ['neq', 3];
            //日期
            if(isset($params['add_start']) && $params['add_start'] != '' && isset($params['add_end']) && $params['add_end'] != ''){
                $where['order_time'] = ['between', [$params['add_start'], $params['add_end']]];
            }
            if(isset($params['add_start']) && $params['add_start'] != '' && isset($params['add_end']) && $params['add_end'] == ''){
                $where['order_time'] = ['>=', $params['add_start']];
            }if(isset($params['add_start']) && $params['add_start'] == '' && isset($params['add_end']) && $params['add_end'] != ''){
                $where['order_time'] = ['<=', $params['add_end']];
            }
            //关键字
            if(isset($params['keyword']) && $params['keyword'] != ''){
                $keyword = $params['keyword'];
                $where['a.order_number|a.order_name|a.order_phone'] = array('like',"%$keyword%");
            }
            if(isset($params['province']) && $params['province'] != ''){
                $where['a.order_sheng'] =$params['province'] ;
            }
            if(isset($params['city']) && $params['city'] != ''){
                $where['a.order_shi'] = $params['city'];
            }
            if(isset($params['district']) && $params['district'] != ''){
                $where['a.order_qu'] = $params['district'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count =Db::table('up_order')
                ->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.order_userid','left')
                ->join('up_goods ug','ug.good_id = a.order_good_id','left')
                ->where($where)
                ->count();
            $count2 =Db::table('up_order')
                ->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.order_userid','left')
                ->join('up_goods ug','ug.good_id = a.order_good_id','left')
                ->where($where)
                ->column('order_id');
            $lists = Db::table('up_order')
                ->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.order_userid','left')
                ->join('up_goods ug','ug.good_id = a.order_good_id','left')
                ->where($where)
                ->field('a.*,uuu.user_id,uuu.user_username')
                ->order('order_time DESC')
                ->page($Nowpage,$limits)
                ->select();
            foreach ($lists as $key=>&$vo){
                $district =Db::table('up_area')->where('district_id',$vo['order_sheng'])->value('district');
                $district1 =Db::table('up_area')->where('district_id',$vo['order_shi'])->value('district');
                $district2 =Db::table('up_area')->where('district_id',$vo['order_qu'])->value('district');
                $vo['address'] = $district.$district1.$district2.$vo['order_address'];
                if($vo['order_is_coupon']==1){//使用优惠卷
                    $coupon = Db::table('up_my_coupon')->alias('a')
                        ->join('up_coupon uc','uc.coupon_id = a.mc_coupon_id','left')
                        ->field('uc.coupon_name,uc.coupon_price,uc.coupon_start,uc.coupon_end')
                        ->where('a.mc_id',$vo['order_my_coupon'])
                        ->find();
                    $vo['coupon_name'] = $coupon['coupon_name'];
                    $vo['coupon_price'] = $coupon['coupon_price'];
                    $vo['time'] = date('Y-m-d',strtotime($coupon['coupon_start'])).' 至 '.date('Y-m-d',strtotime($coupon['coupon_end']));
                }else{//没有使用优惠卷
                    $vo['coupon_name'] = '';
                    $vo['coupon_price'] = '';
                    $vo['time'] = '';
                }
                if($vo['order_type']==1){
                    $vo['order_type_name'] = '支付宝';
                }elseif ($vo['order_type']==2){
                    $vo['order_type_name'] = '微信';
                }elseif ($vo['order_type']==3){
                    $vo['order_type_name'] = '金币';
                }
            }
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count2' => $str, 'data' => $lists]);
        }
        if(session('?order_id1')){
            session('order_id1',null);
        }
        $province = Db::table('up_area')->where('level', 1)->select();
        $this->assign('province', $province);
        return $this->fetch();
    }

    /***
     * 订单导出
     */
    public function excelIn(){
        $ids = input('param.ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择数据']);
        }
        $where['order_id'] = array('IN',$ids);
        $lists = Db::table('up_order')
            ->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.order_userid','left')
            ->join('up_goods ug','ug.good_id = a.order_good_id','left')
            ->where($where)
            ->field('a.*,uuu.user_id,uuu.user_username')
            ->order('order_time DESC')
            ->select();
        foreach ($lists as $key=>&$vo){
            $district =Db::table('up_area')->where('district_id',$vo['order_sheng'])->value('district');
            $district1 =Db::table('up_area')->where('district_id',$vo['order_shi'])->value('district');
            $district2 =Db::table('up_area')->where('district_id',$vo['order_qu'])->value('district');
            $vo['address'] = $district.$district1.$district2.$vo['order_address'];
            if($vo['order_is_coupon']==1){//使用优惠卷
                $coupon = Db::table('up_my_coupon')->alias('a')
                    ->join('up_coupon uc','uc.coupon_id = a.mc_coupon_id','left')
                    ->field('uc.coupon_name,uc.coupon_price,uc.coupon_start,uc.coupon_end')
                    ->where('a.mc_coupon_id',$vo['order_my_coupon'])
                    ->find();
                $vo['coupon_name'] = $coupon['coupon_name'];
                $vo['coupon_price'] = $coupon['coupon_price'];
                $vo['time'] = $coupon['coupon_start'].' '.$coupon['coupon_end'];
            }else{//没有使用优惠卷
                $vo['coupon_name'] = '';
                $vo['coupon_price'] = '';
                $vo['time'] = '';
            }
            if($vo['order_type']==1){
                $vo['order_type_name'] = '支付宝';
            }elseif ($vo['order_type']==2){
                $vo['order_type_name'] = '微信';
            }elseif ($vo['order_type']==3){
                $vo['order_type_name'] = '金币';
            }
            unset($lists[$key]['order_num']);
            unset($lists[$key]['order_remark']);
            unset($lists[$key]['order_type']);
            unset($lists[$key]['order_status']);
            unset($lists[$key]['order_back_transaction_id']);
            unset($lists[$key]['order_sendtime']);
            unset($lists[$key]['order_paydate']);
            unset($lists[$key]['order_del']);
            unset($lists[$key]['order_suretime']);
            unset($lists[$key]['order_good_id']);
            unset($lists[$key]['order_hh_payment']);
            unset($lists[$key]['order_glod']);
            unset($lists[$key]['order_address']);
            unset($lists[$key]['order_is_coupon']);
            unset($lists[$key]['order_my_coupon']);
            unset($lists[$key]['order_freight_money']);
            unset($lists[$key]['order_all_price']);
            unset($lists[$key]['order_express_name']);
            unset($lists[$key]['order_express_num']);
            unset($lists[$key]['order_j_glod']);
            unset($lists[$key]['order_sheng']);
            unset($lists[$key]['order_shi']);
            unset($lists[$key]['order_qu']);
            unset($lists[$key]['user_id']);
            unset($lists[$key]['order_qu']);
            unset($lists[$key]['order_send']);
            unset($lists[$key]['order_after']);
            $vo['order_user_pay1'] = $vo['order_user_pay'];
            $vo['order_number'] =  "\t".$vo['order_number']."\t";
            $data1 =Db::table('up_order_info')->alias('a')
                ->join('up_goods ug','ug.good_id = a.oi_good_id','left')
                ->where('a.oi_order_id',$vo['order_id'])
                ->field('ug.good_name,ug.good_isbn')
                ->select();
            $vo['good_name'] = implode(',',array_column($data1,'good_name'));
            $vo['good_isbn'] = implode(',',array_column($data1,'good_isbn'));
            unset($lists[$key]['order_id']);

        }
        $cellname = [
            ['order_number','订单号',20,'LEFT'],
            ['order_userid','用户ID',15,'LEFT'],
            ['order_user_pay','订单金额',15,'LEFT'],
            ['order_time','下单时间',15,'LEFT'],
            ['order_paytime','支付时间',15,'LEFT'],
            ['order_name','收货人',15,'LEFT'],
            ['order_phone','收货手机号码',15,'LEFT'],
            ['order_telphone','座机号码',15,'LEFT'],
            ['user_username','用户名字',15,'LEFT'],
            ['address','地址',15,'LEFT'],
            ['coupon_name','优惠卷名称',15,'LEFT'],
            ['coupon_price','优惠卷价格',15,'LEFT'],
            ['time','优惠劵有效期',15,'LEFT'],
            ['order_type_name','支付方式',15,'LEFT'],
            ['order_user_pay1','支付金额',15,'LEFT'],
            ['good_name','商品名称',15,'LEFT'],
            ['good_isbn','ISBN',15,'LEFT'],
        ];
        $res = exportExcel('待发货订单列表','',$cellname,$lists);
        return json($res);
    }
    /***
     * 查看商品
     */
    public function detail(){
        if(request()->isAjax()){
            $id = input('param.id');
            $where['oi_order_id'] = $id;
            $count = Db::table('up_order_info')->where($where)->count();
            $lists = Db::table('up_order_info')->where($where)->select();
            foreach ($lists as $key=>&$vo){
                if($vo['oi_type']==1){
                    $info = Db::table('up_good_spec')->where('spec_id',$vo['oi_spec_id'])->find();
                    $vo['spec'] = $info['spec_name'];
                    $vo['price'] = $vo['oi_price']*$vo['oi_num'].'人民币';
                }else{
                    $vo['spec'] = '';
                    $vo['price'] = $vo['oi_price']*$vo['oi_num'].'金币';
                }
                $good_isbn = Db::table('up_goods')->where('good_id',$vo['oi_good_id'])->value('good_isbn');
                $vo['good_isbn'] = $good_isbn;
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $id = input('id');
        if($id != ''){
            session('order_id1',$id);
        }
        return $this->fetch();
    }

    /***
     * 待发货订单导出
     */
    public function excelgood(){
        $ids = input('param.ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择数据']);
        }
        $where['oi_id'] = array('IN',$ids);
        $lists = Db::table('up_order_info')->where($where)->select();
        foreach ($lists as $key=>&$vo){
            if($vo['oi_type']==1){
                $info = Db::table('up_good_spec')->where('spec_id',$vo['oi_spec_id'])->find();
                $vo['spec'] = $info['spec_name'];
                $vo['price'] = $vo['oi_price']*$vo['oi_num'].'人民币';
            }else{
                $vo['spec'] = '';
                $vo['price'] = $vo['oi_price']*$vo['oi_num'].'金币';
            }
            $res1 = Db::table('up_goods')->where('good_id',$vo['oi_good_id'])->field('good_name,good_isbn')->find();
            unset($lists[$key]['oi_id']);
            unset($lists[$key]['oi_order_id']);
            unset($lists[$key]['oi_good_thumb']);
            unset($lists[$key]['oi_good_coding']);
            unset($lists[$key]['oi_pay_time']);
            unset($lists[$key]['oi_type']);
            unset($lists[$key]['oi_spec_id']);
            unset($lists[$key]['oi_after_status']);
            unset($lists[$key]['after_return_id']);
            unset($lists[$key]['after_exchange_id']);
            unset($lists[$key]['after_reason']);
            unset($lists[$key]['after_status']);
            unset($lists[$key]['after_num']);
            unset($lists[$key]['after_time']);
            unset($lists[$key]['after_user_courier']);
            unset($lists[$key]['after_user_num']);
            unset($lists[$key]['after_us_courier']);
            unset($lists[$key]['after_us_num']);
            unset($lists[$key]['after_user_phone']);
            unset($lists[$key]['after_user_address']);
            unset($lists[$key]['after_spec_id']);
            unset($lists[$key]['after_price']);
            unset($lists[$key]['after_gold']);
            unset($lists[$key]['after_allow_time']);
            unset($lists[$key]['after_false_reason']);
            unset($lists[$key]['price']);
            $vo['good_isbn'] =$res1['good_isbn'];
        }
        $order_number = Db::table('up_order')->where('order_id',session('order_id'))->value('order_number');
        $cellname = [
            ['oi_good_id','商品ID',20,'LEFT'],
            ['oi_price','商品单价',20,'LEFT'],
            ['oi_num','购物数量',15,'LEFT'],
            ['oi_good_name','商品名称',20,'LEFT'],
            ['spec','商品规格',15,'LEFT'],
            ['price','商品总价格',15,'LEFT'],
            ['good_isbn','ISBN',15,'LEFT'],
        ];
        $res = exportExcel('待发货订单'.$order_number,$order_number,$cellname,$lists);
        return json($res);
    }
    /***
     * 待发货订单发货
     */
    public function commodity(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $order_after = Db::table('up_order')->where('order_id', $id)->value('order_after');
            if($order_after == 2) return json(['code' => 100, 'msg' => '该订单已申请售后，不允许发货']);
            $data = array();
            $data['order_express_name'] = input('param.order_express_name'); //快递公司
            $data['order_express_num'] = input('param.order_express_num'); //快递单号
            $data['order_sendtime'] = date('Y-m-d H:i:s',time()); //发货时间
            $data['order_status'] = 3; //快递单号
//            writelog (session ('uid') , session ('username') , $id . '号订单完善了发货信息！' , 1);
            $res = Db::table('up_order')->where('order_id',$id)->update($data);
            if($res){
//                writelog (session ('uid') , session ('username') , $id . '号订单修改完成了发货信息！' , 1);
                $user_id = Db::table('up_order')->where('order_id', $id)->value('order_userid');
                $user_id = (string)$user_id;
                try{
                    $this->Jpush(1, $user_id, '喵喵阅读：您有一笔订单已发货', ['send_type' => 3, 'order_id' => $id]);
                }catch(\Exception $e){

                }
                return json(['code' => 200, 'msg' => '发货成功']);
            }else{
                return json(['code' => 100, 'msg' => '发货失败']);
            }
        }
        $id = input('param.id');
        $this->assign('id',$id);
//        writelog (session ('uid') , session ('username') , $id . '号订单点击了发货按钮！' , 1);
        return $this->fetch();
    }
}